package com.ruoyi.web.controller.system;

import java.util.List;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.system.domain.AdminBook;
import com.ruoyi.system.domain.AdminPreidCourse;
import com.ruoyi.system.domain.AdminSong;
import com.ruoyi.system.service.IAdminBookService;
import com.ruoyi.system.service.IAdminPreidCourseService;
import com.ruoyi.system.service.IAdminSongService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 教材Controller
 * 
 * @author ruoyi
 * @date 2020-06-10
 */
@Controller
@RequestMapping("/system/book")
public class AdminBookController extends BaseController
{
    private String prefix = "system/book";

    @Autowired
    private IAdminBookService adminBookService;
  @Autowired
  private IAdminPreidCourseService adminPreidCourseService;
  @Autowired
  private IAdminSongService adminSongService;

    @RequiresPermissions("system:book:view")
    @GetMapping()
    public String book()
    {
        return prefix + "/book";
    }

    /**
     * 查询教材列表
     */
    @RequiresPermissions("system:book:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(AdminBook adminBook)
    {
        startPage();
        adminBook.setUserId(ShiroUtils.getUserId());
        List<AdminBook> list = adminBookService.selectAdminBookList(adminBook);
        return getDataTable(list);
    }

    /**
     * 导出教材列表
     */
    @RequiresPermissions("system:book:export")
    @Log(title = "教材", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(AdminBook adminBook)
    {
        List<AdminBook> list = adminBookService.selectAdminBookList(adminBook);
        ExcelUtil<AdminBook> util = new ExcelUtil<AdminBook>(AdminBook.class);
        return util.exportExcel(list, "book");
    }
    
    /**
     * 新增教材
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存教材
     * @throws Exception 
     */
    @RequiresPermissions("system:book:add")
    @Log(title = "教材", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(AdminBook adminBook) throws Exception
    {
        adminBook.setCompanyId(ShiroUtils.getCompanyId());
        adminBook.setCompanyName(ShiroUtils.getCompanyName());
        adminBook.setUserId(ShiroUtils.getUserId());
        adminBook.setCreateBy(ShiroUtils.getSysUser().getUserName());
        return toAjax(adminBookService.insertAdminBook(adminBook));
    }

    /**
     * 修改教材
     */
    @GetMapping("/edit/{bookId}")
    public String edit(@PathVariable("bookId") Long bookId, ModelMap mmap)
    {
        AdminBook adminBook = adminBookService.selectAdminBookById(bookId);
        mmap.put("adminBook", adminBook);
        return prefix + "/edit";
    }

    /**
     * 修改保存教材
     * @throws Exception 
     */
    @RequiresPermissions("system:book:edit")
    @Log(title = "教材", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(AdminBook adminBook) throws Exception
    {
    	adminBook.setUpdateBy(ShiroUtils.getLoginName());
        return toAjax(adminBookService.updateAdminBook(adminBook));
    }

    /**
     * 删除教材
     */
    @RequiresPermissions("system:book:remove")
    @Log(title = "教材", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(adminBookService.deleteAdminBookByIds(ids));
    }
    
    /**
     * 教材状态修改
     */
    @Log(title = "教材管理", businessType = BusinessType.UPDATE)
    @RequiresPermissions("system:book:edit")
    @PostMapping("/changeStatus")
    @ResponseBody
    public AjaxResult changeStatus(AdminBook adminBook)
    {
        return toAjax(adminBookService.changeStatus(adminBook));
    }
    
    /**
     * 跳转曲目列表
     */
    @RequiresPermissions("system:book:list")
    @GetMapping("/detail/{bookId}")
    public String detail(@PathVariable("bookId") Long bookId, ModelMap mmap)
    {	
    	mmap.put("bookId", bookId);
        return "system/song/song";
    }


  /**
   * 我的课程- 添加教材-教材列表
   * @param preidId
   * @param mmap
   * @return
   */
    @RequiresPermissions("system:book:view")
    @GetMapping("/{preidId}")
    public String book2(@PathVariable("preidId") Long preidId, ModelMap mmap)
    {
      mmap.put("preidId", preidId);
      return prefix + "/book2";
    }

    /**
     * 查询教材列表
     */
    @RequiresPermissions("system:book:list")
    @PostMapping("/list2")
    @ResponseBody
    public TableDataInfo list2(AdminBook adminBook)
    {
      startPage();
      adminBook.setUserId(ShiroUtils.getUserId());
      List<AdminBook> list = adminBookService.selectAdminBookList(adminBook);

      AdminPreidCourse adminPreidCourse = new AdminPreidCourse();
      AdminSong adminSong = new AdminSong();
      for (AdminBook book : list) {
        adminPreidCourse.setBookId(book.getBookId());
        adminPreidCourse.setPreidId(adminBook.getPreidId());
        adminPreidCourse.setCreateBy(ShiroUtils.getSysUser().getUserName());
        List<AdminPreidCourse> adminPreidCourses = adminPreidCourseService.selectAdminPreidCourseList(adminPreidCourse);
        if(null!=adminPreidCourse && adminPreidCourses.size()>0){
          adminSong.setBookId(book.getBookId());
          List<AdminSong> listSong = adminSongService.selectAdminSongList(adminSong);
          if(null!=listSong){
            if(listSong.size()==adminPreidCourses.size()){
              book.setAddAllSongFlag("1");//全部添加
            }
          }
        }

      }
      return getDataTable(list);
    }

    /**
     * 跳转曲目列表
     */
    @RequiresPermissions("system:book:list")
    @GetMapping("/detail2/{bookId}/{preidId}")
    public String detail2(@PathVariable("bookId") Long bookId,@PathVariable("preidId") Long preidId, ModelMap mmap)
    {
      mmap.put("bookId", bookId);
      mmap.put("preidId", preidId);
      return "system/song/song2";
    }

    /**
     * 新增保存教材下的所有曲目
     * @throws Exception
     */
    @RequiresPermissions("system:book:add")
    @Log(title = "教材", businessType = BusinessType.INSERT)
    @PostMapping("/add2")
    @ResponseBody
    public AjaxResult addSave2(AdminBook adminBook) throws Exception
    {
      AdminPreidCourse adminPreidCourse = new AdminPreidCourse ();
      adminPreidCourse.setCourseType("1");
      adminPreidCourse.setPreidId(adminBook.getPreidId());
      adminPreidCourse.setCreateBy(ShiroUtils.getSysUser().getUserName());

      AdminSong adminSong = new AdminSong();
      adminSong.setBookId(adminBook.getBookId());
      List<AdminSong> list = adminSongService.selectAdminSongList(adminSong);
      if(null!=list && list.size()>0){
        for (AdminSong song : list) {
          adminPreidCourse.setCourseId(song.getSongId());
          adminPreidCourse.setCourseName(song.getSongName());
          adminPreidCourse.setBookId(adminBook.getBookId());
          adminPreidCourseService.insertAdminPreidCourse(adminPreidCourse);
        }
      }
      return toAjax(true);
    }
}
